package org.flown.shop.service;

import org.flown.shop.dao.SellerDAO;
import org.flown.shop.dto.SellerDTO;
import org.flown.shop.entity.Seller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class SellerService extends BaseService<Seller>
{
    private final SellerDAO dao_seller;

    @Autowired
    public SellerService(SellerDAO dao_seller) {
        super(dao_seller);
        this.dao_seller = (SellerDAO) this.dao;
    }


    private Seller getSellerByID(int id) {return this.selectByID(id);}
    public SellerDTO getSellerDTOByID(int id) {return new SellerDTO(this.selectByID(id));}


    /**
     * account
     * password
     * */
    public int insertSeller(Seller seller)
    {
        if (this.getSellerByAccount(seller.getAccount()) != null)
            return 0;
        return this.insert(seller);
    }
    public boolean updateSellerByID(Seller original_seller) {return this.updateByID(original_seller);}
    public int deleteSellerByID(int id) {return this.deleteByID(id);}


    private Seller getSellerByAccount(String account) {return this.dao_seller.selectByAccount(account);}
    public SellerDTO getSellerDTOByAccount(String account) {return new SellerDTO(this.dao_seller.selectByAccount(account));}


    /**
     * nickname 可空
     * account
     * password
     * */
    public ServiceResult register(Seller seller)
    {
        if (this.insertSeller(seller) == 0)
            return ServiceResult.FAILED;
        System.out.println(this + ".register: 注册成功");
    	return ServiceResult.SUCCESS;
    }


    /**
     * account
     * password
     * */
    public ServiceResult login(Seller seller)
    {
        Seller user = this.getSellerByAccount(seller.getAccount());
        if (user == null)
        {
            System.out.println(this + ".login: 账户不存在");
            return ServiceResult.NOT_EXISTS;
        }
        if (!user.getPassword().equals(seller.getPassword()))
        {
            System.out.println(this + ".login: 密码错误");
            return ServiceResult.FAILED;
        }

        System.out.println(this + ".login: 登陆成功");
        return ServiceResult.SUCCESS;
    }


}
